var editUserUrl = null;
var editUserMethod = null;

$(function () {
    $('#dg_user').datagrid({
                        url: ctx+'/sys/user/search_page_easyui',
                        method:'get'
                    })
});

var addUser = function () {
    editUserUrl = ctx + 'sys/user';
    editUserMethod = 'post';
    $('#form_user_edit').form('clear');
    $('#form_user_edit select[name="state"]').val(1);
    $('#dl_user_edit').dialog('open');
}

var updateUser = function () {
    var selected = $('#dg_user').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的用户");
        return;
    }
    $('#form_user_edit').form('load', selected);

    editUserUrl = ctx + '/sys/user';
    editUserMethod = 'put';
    $('#dl_user_edit').dialog('open');
}

var closeUserEdit = function () {
    $('#dl_user_edit').dialog('close');
}

var submitUser = function () {
    if (!$('#form_user_edit').form("validate")) {
        return;
    }
    $.ajax({
               url: editUserUrl,
               type: editUserMethod,
               contentType: 'application/json',
               dataType: 'json',
               data: JSON.stringify($('#form_user_edit').serializeJSON()),
               success: function (data) {
                   closeUserEdit();
                   //刷新表格
                   $('#dg_user').datagrid("reload");
               }
           });
}

var removeUser = function () {
    var selected = $('#dg_user').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的用户");
        return;
    }
    $.messager.confirm("警告", "您确定要删除用户“" + selected.nick + "”吗？", function (r) {
        if (!r) {
            return;
        }
        $.ajax({
                   url:ctx+'sys/user/'+selected.id,
                   type:'delete',
                   success:function (data) {
                       $('#dg_user').datagrid('reload');
                   }
               });
    })
}

var searchUser = function () {
    $('#dg_user').datagrid('load', {
        'id': $('#searUserParams input[name="id"]').val(),
        'nick': $('#searUserParams input[name="nick"]').val(),
        'account': $('#searUserParams input[name="account"]').val()
    });
}